package com.tsd.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tsd.core.interceptor.AccountSetInterceptor;
import com.tsd.core.interceptor.ReqParamInterceptor;
import com.tsd.core.interceptor.CorsInterceptor;
import com.tsd.interceptor.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author Hillpool
 * @Date 2019/3/11 16:48
 * @Modefied By:
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
    @Resource
    private LogInterceptor logInterceptor;
    @Resource
    private CorsInterceptor corsInterceptor;
    @Resource
    private AccountSetInterceptor accountSetInterceptor;
    @Resource
    private ReqParamInterceptor reqParamInterceptor;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //json转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        List<MediaType> mediaTypeList = new ArrayList<MediaType>();
        mediaTypeList.add(new MediaType("application", "json", StandardCharsets.UTF_8));
        mediaTypeList.add(new MediaType("text", "plain", StandardCharsets.UTF_8));
        mediaTypeList.add(new MediaType("text", "*", StandardCharsets.UTF_8));
        mediaTypeList.add(new MediaType("*", "*", StandardCharsets.UTF_8));
        mediaTypeList.add(MediaType.TEXT_PLAIN);
        converter.setSupportedMediaTypes(mediaTypeList);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        converter.setObjectMapper(mapper);
        converters.add(converter);
    }

    /**
     * 添加静态资源文件，外部可以直接访问地址
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/**").addResourceLocations("/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
        //依次是切换账套、请求参数、日志记录的拦截器，按照顺序执行
        registry.addInterceptor(accountSetInterceptor).addPathPatterns("/**");
        registry.addInterceptor(reqParamInterceptor).addPathPatterns("/**");
        registry.addInterceptor(logInterceptor).addPathPatterns("/**");
    }
    private void useSample(InterceptorRegistry registry) {
        //拦截器复杂的用法示例
        List<String> excludePathList = new ArrayList<>();
        excludePathList.add("/admin/auth/login");
        excludePathList.add("/admin/auth/captchaImage");
        //文件访问的有其自主处理
        excludePathList.add("/file/**");
        registry.addInterceptor(accountSetInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathList);
    }
}
